%% Author: vivimice
%% Created: Jan 10, 2012
%% Description: TODO: Add description to welcome
-module(login_screen).

%%
%% Include files
%%
-include("tstring.hrl").

%%
%% Exported Functions
%%
-export([show/1]).

%%
%% API Functions
%%
show(Context) ->
	login(Context).

%%
%% Local Functions
%%
login(Context) ->
	
	tio_manager:format(Context, "User Name: ", []),

	UserName = input_field:get_text(
				 input_field:set_validator(
				   input_field:set_max_width(
					 input_field:new_config(Context),
					 20),
				   fun (S) when ?IS_TSTRING(S) -> if ?TSTRING_LENGTH(S) == 0 -> {error, ?TSTRING("Plz enter username")}; true -> ok end end)),
	tio_manager:write_newline(Context),
	
	UserExists = user_service:user_exists(tstring:to_binary(UserName)),
	if
		UserExists ->
			tio_manager:format(Context, "Password: ", []),
			_ = input_field:get_text(Context, input_field:create_options(password, 12)),
			tio_manager:write_newline(Context);
		
		% user not exists, request register
		true ->
			tio_manager:format(Context, "User ~s not exists. ", [UserName]),
			
			OptBack = select_field:create_option(tstring:value_of("Back"), back),
			OptReg = select_field:create_option(tstring:value_of("Register"), register),
			case select_field:get_choice(Context, select_field:create_config([OptBack, OptReg])) of
				back ->
					tio_manager:write_newline(Context),
					login(Context);
				
				register -> 
					register(Context)
			end
	end,
	ok.

register(Context) ->
	ok.
